using System;
using System.Collections.Generic;
using UnityEngine;

public class PlayerSettingsLayer : UILayer
{
	public UITable table;

	public new UILabel name;

	public UICenterOnChild center;

	public GameObject flagPrefab;

	public Dictionary<GameObject, GameConstants.Country> countryMap = new Dictionary<GameObject, GameConstants.Country>();

	public UIInput input;

	public UILabel title;

	private Action onDone;

	public UISprite notificationButton;

	public UILabel notificationLabel;

	private GGPlayerSettings player;

	private int tableReposition;

	protected Transform originalSelectedFlagTransform;

	protected bool mustSelectNameAndFlag = true;

	private const string defaultName = "Enter your name";

	public void ShowFirstTime()
	{
		mustSelectNameAndFlag = true;
	}

	public void SetTitle(string text)
	{
		title.text = text;
	}

	public void SetOnDone(Action onComplete)
	{
		onDone = onComplete;
	}

	private void Awake()
	{
		List<GameConstants.Country> countries = GameConstants.Instance.Countries;
		GameObject parent = table.gameObject;
		player = GGPlayerSettings.instance;
		if (player.Model.name != null && player.Model.name != "You")
		{
			name.text = player.Model.name;
		}
		else
		{
			name.text = "Enter your name";
		}
		foreach (GameConstants.Country item in countries)
		{
			GameObject gameObject = NGUITools.AddChild(parent, flagPrefab);
			gameObject.name = item.countryName;
			UISprite component = gameObject.GetComponent<UISprite>();
			UILabel componentInChildren = gameObject.GetComponentInChildren<UILabel>();
			componentInChildren.text = item.countryName;
			component.spriteName = item.spriteName;
			countryMap.Add(gameObject, item);
			if (item.flag == (GameConstants.Flags)player.Model.flag)
			{
				originalSelectedFlagTransform = gameObject.transform;
			}
		}
	}

	private void OnEnable()
	{
		tableReposition = 0;
		Ads.instance.hideBanner(true);
		if (player.Model.name != null && player.Model.name != "You")
		{
			name.text = player.Model.name;
		}
		else
		{
			name.text = "Enter your name";
		}
		Refresh();
	}

	private void OnDisable()
	{
		Ads.instance.hideBanner(false);
		mustSelectNameAndFlag = false;
	}

	public override void Update()
	{
		if (tableReposition++ < 10)
		{
			table.Reposition();
			return;
		}
		if (tableReposition++ == 11)
		{
			center = table.GetComponent<UICenterOnChild>();
		}
		if (center.centeredObject == null)
		{
			center.CenterOn(originalSelectedFlagTransform);
		}
	}

	public void OnDone()
	{
		if ((string.IsNullOrEmpty(name.text) || name.text == "Enter your name") && mustSelectNameAndFlag)
		{
			UIDialog.instance.ShowOk("Please Enter a Name", "What do we call you?", "Ok", null);
			return;
		}
		center.Recenter();
		GameObject centeredObject = center.centeredObject;
		if (countryMap.ContainsKey(centeredObject))
		{
			GameConstants.Country country = countryMap[centeredObject];
			player.PickFlag((int)country.flag);
		}
		string text = name.text;
		if (text == "Enter your name")
		{
			text = null;
		}
		player.SetName(text);
		NavigationManager.instance.Pop();
		if (onDone != null)
		{
			onDone();
		}
	}

	private void Refresh()
	{
		if (player.Model.notificationsOff)
		{
			notificationButton.spriteName = "btn-red";
			notificationLabel.text = "Notifications:\n Off";
		}
		else
		{
			notificationButton.spriteName = "btn-green";
			notificationLabel.text = "Notifications:\n On";
		}
	}

	public void OnNotifications()
	{
		GGPlayerSettings.instance.Model.notificationsOff = !GGPlayerSettings.instance.Model.notificationsOff;
		GGPlayerSettings.instance.Save();
		if (GGPlayerSettings.instance.Model.notificationsOff)
		{
			Analytics.instance.ReportNotificationsOff();
		}
		Refresh();
	}
}
